package com.cxs.controller;

import com.cxs.model.Book;
import com.cxs.service.BookService;
import cxs.utils.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;

/*
 * @Author:cxs
 * @Date:2021/9/30  22:03
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@RestController
@RequestMapping("/book")
@Api(tags = "书处理器")
public class BookController {
    @Autowired
    private BookService bookService;


    @RequestMapping(value = "/list/{pageNum}/{pageSize}/{cid}",method = RequestMethod.GET)
    @ApiOperation(value = "分页查询书籍")
    public Result<Book> list(
            @PathVariable("pageNum") @ApiParam("查看的页码") Integer pageNum,
            @PathVariable("pageSize") @ApiParam("每页显示条数") Integer pageSize,
            @PathVariable("cid") @ApiParam("分类id") Integer cid){
        if(pageNum == null) pageNum = 1;
        if(pageSize == null) pageSize = 7;
        if(cid == null) {
            return Result.failare("请求出错");
        }
        if(cid == -1){
            // 不分类
            return Result.OK("查询成功",bookService.listByPage(pageNum,pageSize));
        }else{
            // 分类
            return Result.OK("查询成功",bookService.listByPageAndCid(pageNum, pageSize, cid));
        }
    }

    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除书籍")
    public Result<Book> delete(@PathVariable("id") @ApiParam("书籍id") Integer id){
        if(id == null) {
            return Result.failare("请求出错");
        }
        return bookService.delete(id);
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "添加书籍")
    public Result<Book> add(@RequestBody @ApiParam("书籍对象") Book book){
        if(book == null || book.getReqDate() == null){
            return Result.failare("系统抛锚了...");
        }
        if(book.getId() == null){
            // 添加
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                book.setCreatedate(sdf.parse(book.getReqDate()));
                return bookService.add(book);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                book.setCreatedate(sdf.parse(book.getReqDate()));
                return bookService.update(book);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return Result.failare("系统抛锚了...");
    }

    @RequestMapping(value = "/preupdate",method = RequestMethod.GET)
    @ApiOperation(value = "修改前数据回显")
    public Result<Book> preupdate(@ApiParam("书籍id") Integer id){
        if(id == null){
            return Result.failare("系统抛锚了...");
        }
        return bookService.getOne(id);
    }
}
